<?php
namespace app\viphry\model;

use think\Model;
use think\db;
class AgentMember extends Model
{
    /**
     * 查询代理商信息
     * @param $where where
     * @param $field field
     * @author hey_heyang(635870453@qq.com)
     */
    public function getUserInfo($where,$field = '*') {
        $res = $this->field($field)->where($where)->find();
        return empty($res)?array():$res->toArray();
    }

    /**
     * 查询代理商今日旗下会员
     * @param $agentMemberId 代理商ID
     * @author hey_heyang(635870453@qq.com)
     */
    public function getTodayMemberList($agentMemberId){
        $gx=DB::name('b_gx');
        $todayStart= date('Y-m-d 00:00:00', time());
        $todayEnd= date('Y-m-d 23:59:59', time());
        $where['tm.create_time'] = array('between',$todayStart.','.$todayEnd);
        $where['gx.parent_id'] = $agentMemberId;
        $mModel = new Member();
        $data = $mModel
            ->field('tm.*')
            ->alias('tm')
            ->join('tf_b_gx gx', 'tm.id=gx.user_id')
            ->where($where)
            ->select();
        $memberList = empty($data)?array():$data->toArray();
        foreach($memberList as &$row){
            $row['parent_name'] = $mModel->where(['id'=>$row['parentId']])->value('username');
            $row['parent_mobile'] = $mModel->where(['id'=>$row['parentId']])->value('mobile');
        }
        $total =$gx->where(['parent_id'=>$agentMemberId])->count();
        return array('memberList'=>$memberList,'memberTotal'=>$total);
    }

    /**
     * 查询代理商旗下指定会员
     * @param $agentMemberId 代理商ID
     * @author hey_heyang(635870453@qq.com)
     */
    public  function getTargetList($agentMemberId,$serach){
        $mModel = new Member();
        $where['gx.parent_id'] = $agentMemberId;
        if(is_numeric($serach)){
            $where['tm.mobile'] = ['eq',$serach];
        }else{
            $where['tm.username'] = ['like', '%' . $serach . '%'];
        }

        $data = $mModel
            ->field('tm.*')
            ->alias('tm')
            ->join('tf_b_gx gx', 'tm.id=gx.user_id')
            ->where($where)
            ->select();
        $memberList = empty($data)?array():$data->toArray();
        foreach($memberList as &$row){
            $row['parent_name'] = $mModel->where(['id'=>$row['parentId']])->value('username');
            $row['parent_mobile'] = $mModel->where(['id'=>$row['parentId']])->value('mobile');
        }
        return $memberList;
    }

    /**
     * 查询会员订单记录
     * @param $memberId 会员id
     * @author hey_heyang(635870453@qq.com)
     */
    public function getOrderList($memberId){
//        $orderModel=DB::name('order');
        $orderModel=new Order();
        $data = $orderModel
            ->field('or.*,p.project_name')
            ->alias('or')
            ->join('tf_project p', 'or.project_id=p.id')
            ->where(['or.uid'=>$memberId,'or.status'=>['gt',1]])
            ->select();
        $orderList = empty($data)?array():$data->toArray();
        return $orderList;
    }

}